package model;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ShopDao {


    //查找全部商品
    public List<Shop> selectAllShops(String category){
        List<Shop> shops = new ArrayList<>();
        //与数据库建立连接
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try {
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            //1.建立连接
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/taobao?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false","root","dddd");
            System.out.println("连接成功");
            //2.查找数据
            String sql = "select * from shop where category = ?";
            statement = connection.prepareStatement(sql);
            statement.setString(1,category);
            resultSet = statement.executeQuery();
            //3.将查询到的数据放入shop对象，然后再放入shops集合
            while (resultSet.next()){
                Shop shop = new Shop();
                shop.setShopId(resultSet.getInt("shopId"));
                shop.setCategory(resultSet.getString("category"));
                shop.setIcon(resultSet.getString("icon"));
                shop.setShopName(resultSet.getString("shopName"));
                shop.setShopBrief(resultSet.getString("shopBrief"));
                shop.setPrice(resultSet.getDouble("price"));
                shops.add(shop);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return shops;
    }




}
